<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends MY_Controller {

     function __construct() {
     	
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('parser');
        //$this->template->set_template('home');
        $this->template->add_region('new_tours');
        $this->load->model('news_model', 'news', true);
        $this->load->model('product_model', 'product', true);
        //$this->load->model('popup_model', 'popup', true);
        //$this->load->model('ncategory_model', 'ncategory', true);
        //$this->buildFrontEndTopMenu();
        //$this->buildSlider();

        $this->buildNavigation();
        $this->buildSearchByConditionBox();
        $this->buildSupportBox();

		$this->buildSlider();
		$this->buildCartBox();
		$this->buildCompareBox();
		
		
     }

    function index_() {
    	
        $data['base_url'] = base_url();
        
        $data['categories'] = $this->ncategory->getAll(50, 0, 0, 1);
        
        //print_r($data['categories']);
        $index = 0;
        $now = strtotime(date('y-m-d'));
        foreach ($data['categories'] as &$item) {
        	$subcats = $this->ncategory->getAll(100, 0, $item->id_ncategory, 1);
        	$arrCats = array();
        	for($i = 0; $i < count($subcats); $i++){
        		$arrCats[] = $subcats[$i]->id_ncategory;
        	}
        	//$arrCats = implode(',', $arrCats);
        	//echo $arrCats;
        	$news = array();
        	if(count($arrCats) > 0)
        		$news = $this->news->getAllByCategoryIds($arrCats);
            //$news = $this->news->getAll(0, 4, $item->id_ncategory, 1);
            if(count($news) > 0){
                $main_data = $news[0];
                $date_created = strtotime($main_data['date_add']);
               
                if(($now - $date_created)/(60*60*24) <= 1){
                	$main_data['vi_name'] = $main_data['vi_name'] .  '<img src="'.base_url('images/new.gif').'" /img>';
                }
                
                $main_data['image'] = $main_data['image'] != '' ? '<div class="image">
            <a href="#"><img src="'. base_url(NEWS_IMAGE_PATH. $main_data['image']). '"></a>
        </div>' : '<div class="image">
            <a href="#"><img src="'. base_url('images/gold_cash.png'). '"></a>
        </div>';
                $main_data['url'] = site_url(($item->link_rewrite).'/chi-tiet/' . $main_data['id_news']);
                $item->main_news = $this->parser->parse('default/front_end/home/main_news', $main_data, true);
                
                if(count($news) >= 1){
                    $other_news = array();
                    for($i = 1; $i < count($news); $i++){
                    	$other = $news[$i];
                    $date_created = strtotime($other['date_add']);
               
                if(($now - $date_created)/(60*60*24) <= 1){
                	$other['vi_name'] = $other['vi_name'] .  '<img src="'.base_url('images/new.gif').'" /img>';
                }
                    	$other['url'] = site_url(($item->link_rewrite).'/chi-tiet/' . $other['id_news']);
                        $other_news[] = $other;
                    }
                    
                    //echo 'other: ' . count($other_news);
                    if(count($other_news) > 0){
                    	
                        $item->other_news = $this->parser->parse('default/front_end/home/home_other_news', array('news' => $other_news ), true);
        
                    }
                        else
                        $item->other_news = '';
                }
            }
            else{
                unset($data['categories'][$index]);
                //$item/->main_news = $this->parser->parse('default/front_end/home/main_news', $data, true);
                //$item->other_news = $this->parser->parse('default/front_end/home/home_other_news', $data, true);
            }
            $index++;
        }
       
        $data['popup'] = $this->parser->parse('default/front_end/home/view_popup', array('news' => $other_news ), true);
        $data['base_url'] = base_url();
        $data['site'] =site_url();
        
        $this->template->parse_view('content', 'default/front_end/home/view_home', $data);
        //$this->buildLeftContent();

        $this->template->render();  
    }
    
function index() {
	
        $data['base_url'] = base_url();
        
        $data['products'] = $this->product->getNewProduct(50);
        
        /*
        $popup = $this->popup->getDefault();
        
        if($popup){
        
        	$data['popup'] = $this->parser->parse('default/front_end/home/view_popup', $popup, true);
        }
        */
        
        $lang = getLangCode();
        
        /*
        foreach ($data['products'] as &$item){
        	$item['name'] = $item[$lang . 'name'];
        	$item['image'] = img(array('src' => getDirByDate($item['date_add'], PRODUCT_IMAGE_PATH) . '/' . $item['image'], 'height' ));	
        }
        */
        $data['lang'] =  $lang;
        
        $this->template->parse_view('center_content1', 'default/front_end/home/new_product_view', $data);
        //$this->buildLeftContent();

        $this->template->render();  
    }


    function buildLeftContent() {
        $data['menuitems'] = array(
            array(
                'status' => 'active',
                'click_function' => 'get_list(current_from, this);',
                'text' => 'Add New Child',
                'href' => site_url('children/add_child'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'create_user(this);',
                'text' => 'View All Children ',
                'href' => site_url('children/view_all'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'system_logs(this);',
                'text' => 'View Current Children ',
                'href' => site_url('children/view_current'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'login_logs(this);',
                'text' => 'View Graduated Children ',
                'href' => site_url('children/view_graduated'),
                'subitem' => array('a' => 'a')
            ),
            array(
                'status' => '',
                'click_function' => 'system_config(this);',
                'text' => 'View Withdrawn Children ',
                'href' => site_url('children/view_withdraw'),
                'subitem' => array('a' => 'a')
            )
        );
        $this->load->library('parser');
        $this->parser->parse('default/front_end/menu_left', $data);
        $this->template->parse_view('header', 'default/front_end/menu_left', $data);
    }

}

?>